/*
 * @Author: tomystery
 * @Date:   2017-09-09 15:59:32
 * @Last Modified by:   tomystery
 * @Last Modified time: 2017-09-09 17:09:26
 */
'use strict';
require('./index.css');

require('page/common/nav/index.js');
var navSide = require('page/common/nav-side/index.js');
require('page/common/header/index.js');
var _mm = require('util/mm.js');
var _user = require('service/user-service.js');


//page逻辑部分
var page = {

    init: function() {
        this.onLoad();
        this.bindEvent();

    },


    onLoad: function() {
        //初始化左侧菜单
        navSide.init({
            name: 'pass-update'
        });

    },
    bindEvent: function() {
        var _this = this;
        //点击按钮后的动作
        $('.btn-submit').click(function() {
            var userInfo = {
                    passwordOld: $.trim($('#password').val()),
                    passwordNew: $.trim($('#password-new').val()),
                    passwordConfirm: $.trim($('#password-confirm').val())
                },
                validateResult = _this.validateForm(userInfo);
            if (validateResult.status) {
                //更改用户信息
                _user.updatePassword(userInfo, function(res, msg) {
                	console.log(res);

                    _mm.successTips(msg);
                  

                }, function(errMsg) {
                    console.log(errMsg)
                    _mm.errorTips(errMsg);
                })
            } else {
                _mm.errorTips(validateResult.msg);
            }

        })
    },

 // 验证字段信息
    validateForm : function(formData){
        var result = {
            status  : false,
            msg     : ''
        };
        // 验证密码提示问题答案是否为空
        if(!_mm.validate(formData.passwordOld, 'require')){
            result.msg = '原密码不能为空！';
            return result;
        }
        //验证密码长度
        if (!formData.passwordNew||formData.passwordNew.length<6) {
            result.msg = '密码长度不少于6位';
            return result;
        }
       
        //验证两次密码
        if (formData.passwordNew!== formData.passwordConfirm) {
            result.msg = '两次密码不一致';
            return result;
        }
        // 通过验证，返回正确提示
        result.status   = true;
        result.msg      = '验证通过';
        return result;
    }


};


$(function() {
    page.init();
})